package com.guoguo.controller;

import com.alibaba.fastjson.JSONObject;
import com.guoguo.base.BaseController;
import com.guoguo.base.PageBean;
import com.guoguo.entity.Groups;
import com.guoguo.entity.vo.GroupVO;
import com.guoguo.service.GroupsService;
import com.guoguo.utils.UUIDUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by KLSD on 2017/10/25.
 */
@Controller
@RequestMapping("/view")
public class GroupController extends BaseController {
    @Autowired
    private GroupsService groupsService;

    @RequestMapping(value = "/groupList", method = RequestMethod.GET)
    public ModelAndView Group(int pageNumber, int pageSize) {
        Map<String, Object> data = new HashMap<>();
        PageBean<GroupVO> groupVOs = groupsService.selectgroupList(pageNumber, pageSize);
        data.put("groupList", groupVOs);
        return returnView("/groupList", data);
    }

    /**
     * 跳转添加修改分组
     *
     * @param groupId
     * @return
     */
    @RequestMapping(value = "/saveGroups", method = RequestMethod.GET)
    public ModelAndView saveGroup(String groupId) {
        Map<String, Object> data = new HashedMap();
        if ("".equals(groupId) || groupId == null) {
            data.put("groupId", UUIDUtils.getUUID());
        } else {
            Groups groups = groupsService.selectByPrimaryKey(groupId);
            data.put("groupId", groups.getGroupId());
            data.put("groupName", groups.getGroupName());
            data.put("groupDescription", groups.getGroupDescription());
            data.put("groupType", groups.getGroupType());
        }
        return returnView("/group/groupAdd", data);
    }

    /**
     * 保存添加修改
     *
     * @param groupId
     * @param groupName
     * @param groupDescription
     * @param groupType
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveOrupdateGroup", method = RequestMethod.POST)
    public JSONObject updateGroup(@RequestParam String groupId, @RequestParam String groupName, @RequestParam String groupDescription, @RequestParam int groupType) {
        Groups group = groupsService.addGroup(groupId, groupName, groupDescription, groupType, new Date(), new Date());
        return returnResult(200, group, "");

    }
@ResponseBody
@RequestMapping(value="/searchGroup",method = RequestMethod.POST)
public JSONObject searchBy(@RequestParam String groupName,@RequestParam Integer isShow,@RequestParam String rankType,@RequestParam String orderType,@RequestParam int pageNumber,@RequestParam int pageSize){
    Map<String,Object > data=new HashedMap() ;

    if(rankType.equals("sort") && orderType.equals("desc") ){
        PageBean<GroupVO> groupVos= groupsService.selectBySortDesc(groupName ,isShow ,pageNumber,pageSize);
        data.put("groupList",groupVos);
    }
    else if(rankType .equals("sort")&& orderType .equals("asc") ){
        PageBean<GroupVO> groupVos=groupsService.selectBySortAsc(groupName ,isShow ,pageNumber,pageSize) ;
        data.put("groupList",groupVos);
    }
    else if(rankType .equals("createTime")&& orderType .equals("desc") ){
        PageBean<GroupVO> groupVos=groupsService.selectByCreateTimeDesc(groupName ,isShow ,pageNumber,pageSize) ;
        data.put("groupList",groupVos);
    }
    else if(rankType .equals("createTime")&& orderType .equals("asc") ){
        PageBean<GroupVO> groupVos=groupsService.selectByCreateTimeAsc(groupName ,isShow ,pageNumber,pageSize) ;
        data.put("groupList",groupVos);
    }else{
        data.put("groupList",null);
    }
    return returnResult(200, data,"");

}
}
